/**
 * 
 */
package org.swing.table.model;

import javax.swing.table.DefaultTableModel;

import org.swing.table.util.CheckTable;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lqnhu
 *
 */
public class CheckTableModel<O> extends DefaultTableModel {
	/**
*
*/
	private static final long serialVersionUID = 836875586342144500L;

	public CheckTableModel(String name) {
		addColumn(CheckTable.defaultSelected, new Object[] {});
		addColumn(name, new Object[] {});
	}

	public void setData(java.util.List<O> elements, boolean selected) {
		for (int i = getRowCount() - 1; i >= 0; i--) {
			removeRow(i);
		}
		for (O element : elements) {
			addRow(new Object[] { selected, element });
		}
	}

	public Class<?> getColumnClass(int col) {
		if (col == 0) {
			return Boolean.class;
		}
		return super.getColumnClass(col);
	}

	public boolean isCellEditable(int row, int col) {
		return col == 0;
	}

	public List<O> getFilteredValues() {
		List<O> axioms = new ArrayList<O>();
		for (int i = 0; i < getRowCount(); i++) {
			if (getValueAt(i, 0).equals(Boolean.TRUE)) {
				axioms.add((O) getValueAt(i, 1));
			}
		}
		return axioms;
	}

	public List<O> getAllValues() {
		List<O> values = new ArrayList<O>();
		for (int i = 0; i < getRowCount(); i++) {
			values.add((O) getValueAt(i, 1));
		}
		return values;
	}
}
